package com.xmu.prcs.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.xmu.prcs.action.bean.RetrievalResultBean;
import com.xmu.prcs.dao.PlantFeatureDao;
import com.xmu.prcs.entity.PlantFeature;

/**
 * 实现类
 * @author frequent
 *
 */
public class PlantFeatureDaoImpl extends GenericDaoImpl<PlantFeature> implements PlantFeatureDao{

	public PlantFeatureDaoImpl() {
		super(PlantFeature.class);
	}

	public List<PlantFeature> getByFeatureIds(List<String> featureIds) {
		String sql = "select pf.idplantfeature, pf.maxvalue, pf.minvalue, pf.idfeature, pf.idplant, sum(pf.grade) as grade"
					+ " from plantfeature as pf, plant as p"
					+ " where p.idplant = pf.idplant";
		if(featureIds != null && featureIds.size() != 0){
			sql += " and pf.idfeature in(";
			for(int i = 0 ; i < featureIds.size(); i++){
				if(featureIds.get(i) != null && !featureIds.get(i).equals("")){
					sql += Integer.parseInt(featureIds.get(i)) + ",";
				}
			}
			sql = sql.substring(0, sql.length() - 1);//去掉最后的，
			sql += ")";
		}
		sql += " group by pf.idplant order by grade";
		List<PlantFeature> list = null;
		Query query = getHibernateTemplate().getSessionFactory()
					.getCurrentSession().createSQLQuery(sql).addEntity(PlantFeature.class);
		list = query.list();
		
		return list;
	}

	public List<PlantFeature> getMostImportantFeature(List<String> featureIds) {
		List<PlantFeature> list = null;
		String sql = "select * from PlantFeature as pf"
					+ " where pf.grade=100";
		if(featureIds != null && featureIds.size() != 0){
			sql += " and pf.idfeature in(";
			for(String id : featureIds){
				sql += Integer.parseInt(id) + ",";
			}
			sql = sql.substring(0, sql.length() - 1);//去掉最后的，
			sql += ")";
		}
		Query query = getHibernateTemplate().getSessionFactory()
					.getCurrentSession().createSQLQuery(sql).addEntity(PlantFeature.class);
		list = query.list();
		
		return list;
	}

	public List<PlantFeature> getNotInNumberRange(int featureId, String number) {
		List<PlantFeature> list = null;
		float num = Float.parseFloat(number);
		String sql = "select * from PlantFeature as pf" + " where pf.idfeature=featureId"
					+ " and ( pf.minvalue>" + num + " or pf.maxvalue<" + num + ")"; 

		Query query = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).addEntity(
						PlantFeature.class);
		list = query.list();

		return list;
	}
}
